<template>
  <div>
    <div class="page-root">
      <BasePanel>
        <BaseSearch
          :search="searchProps"
          @searchData="updateTable"
          @resetData="updateTable"
        />
        <div class="sort">
          <div class="sort_title">全部分类：</div>
          <div class="sort_box">
            <div class="sort_commodity">
              <div
                class="sort_item"
                :class="{ sort_item_Active: selectedIndex === item.id }"
                v-for="item in commodity_list"
                :key="item.id"
                @click.stop="toggleCategory(item)"
              >
                <div>
                  <span> {{ item.name }}</span>
                  <img
                    v-if="selectedIndex !== item.id"
                    src="../../assets/img/home/icon_up1.png"
                    alt=""
                  />
                  <img v-else src="../../assets/img/home/icon_up.png" alt="" />
                </div>
              </div>
            </div>
            <div class="sort_commodity2" v-if="type_center.length > 0">
              <div
                class="sort_item"
                :class="{ sort_item_Active2: selectedIndex2 === item.id }"
                v-for="item in type_center"
                :key="item.id"
                @click.stop="toggleCategory2(item)"
              >
                <div>
                  <span> {{ item.name }}</span>
                </div>
              </div>
            </div>
            <div class="sort_commodity3" v-if="type_right.length > 0">
              <div
                class="sort_item"
                :class="{ sort_item_Active3: selectedIndex3 === item.id }"
                v-for="item in type_right"
                :key="item.id"
                @click.stop="toggleCategory3(item)"
              >
                <div>
                  <span> {{ item.name }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
     
        <div class="product_box">
          <div
            class="product_box_item"
            v-for="item in dataSource"
            :key="item.id"
             @click="showDetails(item.id)"
          >
            <div class="product_box_item_img">
              <img :src="processImage(item.image)" alt="" />
            </div>
            <!-- <div v-if="item.type === 1" class="product_identification">
              公开任务
            </div>
            <div v-else class="product_identification2">专属任务</div> -->
            <div class="product_box_cent">
              <div class="product_box_text">
                <div class="product_box_title">{{ item.title }}</div>
              </div>
              <div class="subsidy_ratio">
                <div class="mission_subsidy">
                  <p>
                    <span>{{ item.price[0] }}</span
                    ><span>元</span>
                  </p>
                  <p>价格</p>
                </div>
                <div class="mission_subsidy">
                  <p>
                    <span>{{ item.commission_rate - 0 }}</span
                    ><span>%</span>
                  </p>
                  <p>达人公开佣金</p>
                </div>
                <div class="mission_subsidy">
                  <p>
                    <span>{{ item.open_service_charge - 0 }}</span
                    ><span>%</span>
                  </p>
                  <p>公开服务费</p>
                </div>
              </div>
              <div class="Task_details">
                <div class="shop_name">店铺名称：{{ item.seller_nick_name }}</div>
                <div class="product_name">产品ID: {{ item.goods_id }}</div>
                <div class="sales_volume">是否控佣：{{ item.custom_commission_control === 1 ? '专属控佣' : '专属不控佣' }}</div>
                
                <div class="release_time" v-if="item.custom_commission_control === 1">
                  专属佣金: {{ item.exclusive_commission_rate }}
                </div>
                <div class="end_time"  v-if="item.custom_commission_control === 1">专属服务费: {{ item.exclusive_service_charge_rate }}</div>
              </div>
            </div>
          </div>
          <div v-show="dataSource.length === 0" class="empty">
            <a-empty />
          </div>
        </div>

        <div v-show="dataSource.length > 0" class="page-footer">
          <BasePagination :page-key.sync="pageKey" :get-list="tableInit" />
        </div>
      </BasePanel>
    </div>
   
  </div>
</template>

<script>
import { searchProps1 as searchProps, columns1 as columns } from "./option.js";
import * as Ajax from "@/api/channel";
import { mapGetters } from "vuex";

export default {

  data() {
    return {
      searchProps,
      columns,
      searchForm: {},
      loading: true,
      dataSource: [],
      pageKey: {
        pageSize: 10,
        total: 1,
        current: 1,
      },
      commodity_list: [],
      type_center: [],
      type_right: [],
      selectedIndex: 0,
      selectedIndex2: 0,
      selectedIndex3: 0,
      tabs: [
        {
          id: 1,
          name: "全部任务",
          value: "",
        },
        {
          id: 2,
          name: "公开任务",
          value: 2,
        },
        {
          id: 3,
          name: "今日发布任务",
          value: 1,
        },
        {
          id: 4,
          name: "专属任务",
          value: 3,
        },
      ],
      tabsIndex: 1,
      category_id: null,
      type: "",
      detailsShow: true,
      task_uuid: "",
      goods_category_id:0,
      level:0,
    };
  },
  created() {
    this.tableInit();
    this.getSelect();
  },
  computed: {
    ...mapGetters("user", ["getCdnUrl"]),
  },
  methods: {
    getSelect() {
      Ajax.getCategoryTree().then((res) => {
        if (res.error === 0) {
          res.data.unshift({
            id: 0,
            name: "全部",
            image: '/storage/app_icon/20241227/c92cec8b42b55b01220800c0f2513835.png',
            level: 0,
           
          });

          console.log('processedCategories', res.data);
          this.commodity_list = res.data; 
        }
      });
    },
    updateTable(params = {}) {
      this.searchForm = { ...params };
      this.pageKey = this.$options.data().pageKey;
      this.tableInit();
    },
    // 当天日期
    getFormattedDate(type) {
      const today = new Date();
      const year = today.getFullYear();
      const month = String(today.getMonth() + 1).padStart(2, "0");
      const day = String(today.getDate()).padStart(2, "0");

      if (type === "start") {
        return `${year}-${month}-${day} 00:00:00`;
      } else if (type === "end") {
        return `${year}-${month}-${day} 23:59:59`;
      }
    },
    // table 初始化
    tableInit() {
      this.loading = true;
      const { current, pageSize } = this.pageKey;
      const params = {
        type: this.type,
        goods_category_id: this.goods_category_id,
        level:this.level,
        category_id: this.category_id,
        page: current,
        list_rows: pageSize,
        ...this.searchForm,
      };
      
      delete params.time;
      Ajax.getGoodsList(params).then((res) => {
        if (res.error == 0) {
          this.dataSource = res.data.data;
          console.log('啥时候',this.dataSource.length);
          
          this.pageKey = {
            pageSize: 10,
            total: res.data.total,
            current: res.data.current_page,
          };
        }
        this.loading = false;
      });
    },
    //点击商品类目
    toggleCategory(item) {
      this.type_right = [];
      this.type_center = [];
      this.selectedIndex = "";
      this.selectedIndex = item.id;
      if (item.children) {
        this.type_center = item.children;
      }
      this.goods_category_id = item.id;
      this.level = 0;
      this.tableInit();
    },
    toggleCategory2(item) {
      this.type_right = [];
      this.selectedIndex2 = "";
      this.selectedIndex2 = item.id;
      this.type_right = item.children;
    },
    toggleCategory3(item) {
      this.selectedIndex3 = "";
      this.selectedIndex3 = item.id;
      this.category_id = item.id;
      this.level = 2;
      this.goods_category_id = item.id;
      this.tableInit();
    },
    toggleTabs(item) {
      this.tabsIndex = item.id;
      this.type = item.value;
      this.tableInit();
    },
    //图片校验方法
    processImage(imageUrl) {
      if (imageUrl === undefined || imageUrl === null) {
        return ""; // 或者返回一个默认的 URL
      }

      if (!imageUrl.startsWith("http")) {
        return this.getCdnUrl + imageUrl;
      }
      return imageUrl;
    },
    // 跳转详情
    goToProduct(item) {
      this.task_uuid = item.task_uuid;
      this.detailsShow = false;
      // this.$router.push({
      //   path: "/taskDetails",
      //   query: {
      //     task_uuid: item.task_uuid,
      //   },
      // });
    },
    showDetails(id) {
      this.$emit('show-details', id);
    },
  },
};
</script>

<style lang="scss" scoped>
.BasePanel-root {
  padding: 10px 20px 20px;
}

.tabs-wrap {
  margin-bottom: 10px;
}

.table-wrap {
  margin-top: 16px;
}

.table-top-wrap {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.table-title {
  flex: 1;

  span {
    line-height: 32px;
    font-weight: bold;
    font-size: 16px;
    display: flex;
    align-items: center;

    &::before {
      content: "";
      display: block;
      width: 3px;
      height: 18px;
      background-color: #1890ff;
      margin-right: 8px;
      position: relative;
      top: -1px;
    }
  }
}

.btn-gruop {
  display: flex;
  justify-content: flex-end;

  & > button {
    margin-left: 12px;

    &:first-child {
      margin-left: 0;
    }
  }
}
.sort {
  display: flex;
  width: 100%;
  background: #f5faff;
  border-radius: 10px;
  padding: 14px 20px;
  .sort_title {
    width: 70px;
    font-weight: 400;
    font-size: 14px;
    color: #000000;
  }
  .sort_box {
    .sort_commodity,
    .sort_commodity2,
    .sort_commodity3 {
      width: 1430px;
      overflow-x: auto;
      white-space: nowrap;
      scrollbar-width: none;
      -ms-overflow-style: none;
      &::-webkit-scrollbar {
        display: none;
      }
      &:hover {
        cursor: pointer;
        &::-webkit-scrollbar {
          display: block;
          height: 8px;
          margin-top: 20px;
        }
        &::-webkit-scrollbar-track {
          background: #f1f1f1;
        }
        &::-webkit-scrollbar-thumb {
          background: #fff;
          border-radius: 4px;
        }
        &::-webkit-scrollbar-thumb:hover {
          background: #e5e5e5;
        }
        scrollbar-width: auto;
        -ms-overflow-style: auto;
      }
      .sort_item {
        display: inline-flex;
        margin-left: 26px;
        div {
          display: flex;
          align-items: center;

          span {
            font-weight: 400;
            font-size: 14px;
            color: #333333;
          }
          img {
            width: 8px;
            height: 9px;
            margin-left: 8px;
          }
        }
        &:first-child {
          margin-left: 0;
        }
      }
      .sort_item_Active {
        height: 24px;
        background: #1890ff;
        border-radius: 4px;
        padding: 2px 6px;
        div {
          span {
            font-weight: 400;
            font-size: 14px;
            color: #ffffff;
          }
        }
      }
    }
    .sort_commodity2,
    .sort_commodity3 {
      margin-top: 12px;
      .sort_item {
        display: inline-flex;
        margin-left: 26px;
        height: 24px;
        background: #ffffff;
        border-radius: 4px;
        border: 1px solid #dddddd;
        padding: 2px 6px;
        div {
          span {
            font-weight: 400;
            font-size: 14px;
            color: #999999;
          }
        }
      }
      .sort_item_Active2,
      .sort_item_Active3 {
        height: 24px;
        background: #1890ff;
        border-radius: 4px;
        padding: 2px 6px;
        border: 1px solid #1890ff;
        div {
          span {
            font-weight: 400;
            font-size: 14px;
            color: #ffffff;
          }
        }
      }
    }
  }
}
.tabs {
  display: flex;
  margin-top: 16px;
  .tabs_item {
    height: 34px;
    border-radius: 6px;
    border: 1px solid #cbcbcb;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 400;
    padding: 7px 10px;
    font-weight: 400;
    font-size: 14px;
    color: #666666;
    margin-left: 30px;
    cursor: pointer;
  }
  .tabs_item:first-child {
    margin-left: 0;
  }
  .tabs_item_Active {
    background: #1890ff;
    border: 1px solid #1890ff;
    color: #ffffff;
  }
}
.product_box {
  display: flex;
  flex-wrap: wrap;
  margin-top: 14px;

  .product_box_item {
    position: relative;
    margin-right: 30px;
    margin-bottom: 18px;
    cursor: pointer;
    img {
      width: 224px;
      height: 224px;
      border-radius: 10px 10px 0px 0px;
    }
    .product_identification,
    .product_identification2 {
      width: 64px;
      height: 25px;
      background: #ff790c;
      border-radius: 10px 0px 10px 0px;
      position: absolute;
      top: 0;
      left: 0;
      font-weight: 400;
      font-size: 12px;
      color: #ffffff;
      text-align: center;
      line-height: 25px;
    }
    .product_identification2 {
      background: #e33b35;
    }
    .product_box_cent {
      box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.1);
      border-radius: 0px 0px 10px 10px;
    }
    .product_box_text {
      padding: 3px 8px;
      background-color: #fff;

      border-bottom: 1px dashed #dbdbdb;
      .product_box_title {
        width: 206px;
        text-overflow: ellipsis;
        white-space: nowrap;
        overflow: hidden;
        font-weight: 600;
        font-size: 14px;
        color: #000000;
      }
      .product_box_text_subsidy {
        font-weight: 600;
        font-size: 16px;
        color: #e33b35;
        span {
          font-weight: 400;
          font-size: 11px;
          color: #999999;
        }
        span:nth-child(2) {
          font-weight: 400;
          font-size: 12px;
          color: #e33b35;
        }
      }
      .product_box_text_title {
        width: 206px;
        text-overflow: ellipsis;
        white-space: nowrap;
        overflow: hidden;
        font-weight: 600;
        font-size: 14px;
        color: #000000;
      }
      .target_gross_sales {
        width: 176px;
        height: 22px;
        background: linear-gradient(
          270deg,
          rgba(255, 255, 255, 0) 0%,
          #ffe193 100%
        );
        border-radius: 6px;
        display: flex;
        align-items: center;
        padding: 2px 6px;
        box-sizing: border-box;
        margin-top: 4px;
        margin-bottom: 12px;
        img {
          width: 13px;
          height: 13px;
        }
        span {
          font-weight: 400;
          font-size: 12px;
          color: #9b4309;
        }
        span:nth-child(2) {
          margin: 0 8px 0 4px;
        }
        span:nth-child(3) {
          font-weight: 600;
          font-size: 14px;
          color: #9b4309;
        }
      }
    }
    .subsidy_ratio {
      width: 224px;
      height: 55px;
      background: #f6f5f5;
      border-radius: 0px 0px 10px 10px;
      display: flex;
      padding: 7px 10px;
      box-sizing: border-box;
      .mission_subsidy {
        flex: 1;
        p:nth-child(1) {
          text-align: center;
          margin: 0;
          span:nth-child(1) {
            font-weight: 600;
            font-size: 14px;
            color: #000000;
          }
          span:nth-child(2) {
            font-weight: 400;
            font-size: 9px;
            color: #000000;
          }
        }
        p:nth-child(2) {
          font-weight: 400;
          font-size: 10px;
          color: #999999;
          margin: 0;
          text-align: center;
        }
      }
    }
    .Task_details {
      width: 224px;
      height: 0;
      overflow: hidden;
      position: absolute;
      background-color: #fff;
      box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
      z-index: 1;
      opacity: 0;
      transition: height 0.6s ease, opacity 0.3s ease;
      padding: 8px;
      border-radius: 0 0 10px 10px;
      .shop_name {
        font-weight: 400;
        font-size: 12px;
        color: #666666;
        margin-bottom: 8px;
      }
      .product_name {
        font-weight: 400;
        font-size: 12px;
        color: #666666;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        width: 100%;
        margin-bottom: 8px;
      }
      .sales_volume {
        font-weight: 400;
        font-size: 12px;
        color: #666666;
        margin-bottom: 8px;
      }
      .flex_flexs {
        position: relative;
        overflow: hidden;
        line-height: 20px;
        display: flex;
        width: 190px;
        height: 20px;
        border-radius: 11px;
        border: 1px solid #fc9996;
        font-weight: 400;
        font-size: 20rpx;
        color: #e33b35;
        margin-bottom: 8px;
        .flex_flexs_one {
          width: 1%;
          height: 20px;
          background-color: #fc9996;
        }

        .flex_flexs_txt {
          z-index: 1;
          // top: 4rpx;
          left: 20px;
          position: absolute;
          font-weight: 400;
          font-size: 10px;
          color: #e33b35;
        }
      }
      .release_time,
      .end_time {
        font-weight: 400;
        font-size: 12px;
        color: #666666;
        margin-bottom: 8px;
      }
      .end_time {
        margin-bottom: 0;
      }
    }

    &:hover {
      .Task_details {
        // height: 170px;
        height: 140px;
        opacity: 1;
        background-color: #fff;
      }
      .subsidy_ratio {
        border-radius: 0;
      }
      &:nth-last-child(-n + 5) {
        .Task_details {
          position: relative;
          z-index: 99999;
        }
      }
    }
  }
}
.empty {
  width: 100%;
  text-align: center;
}
</style>
